/*
 *  Copyright (C) 2010 Interactive Media Management
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package dk.i2m.converge.ejb.services;

import dk.i2m.converge.core.ConfigurationKey;
import dk.i2m.converge.core.Notification;
import dk.i2m.converge.core.content.NewsItem;
import dk.i2m.converge.core.newswire.NewswireItem;
import dk.i2m.converge.core.plugin.PluginContext;
import dk.i2m.converge.core.security.UserAccount;
import dk.i2m.converge.core.workflow.Outlet;
import dk.i2m.converge.ejb.facades.NewsItemFacadeLocal;
import dk.i2m.converge.core.search.SearchEngineIndexingException;
import dk.i2m.converge.ejb.facades.SearchEngineLocal;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Implementation of the {@link PluginContext} providing access directly to
 * the EJB services and facades.
 *
 * @author Allan Lykke Christensen
 */
public class PluginContextImpl implements PluginContext {

    private ConfigurationServiceLocal cfgService;

    private NewswireServiceLocal newswireService;

    private NotificationServiceLocal notificationService;

    private UserServiceLocal userService;

    private NewsItemFacadeLocal newsItemFacade;

    private SearchEngineLocal searchEngine;

    /**
     * Creates a new instance of {@link PluginContextImpl)}.
     *
     * @param cfgService
     *          EJB configuration service
     * @param newswireService
     *          EJB newswire service
     * @param notificationService
     *          EJB notification service
     * @param userService
     *          EJB user service
     * @param newsItemFacade
     *          EJB NewsItem facade
     */
    public PluginContextImpl(ConfigurationServiceLocal cfgService, NewswireServiceLocal newswireService, NotificationServiceLocal notificationService, UserServiceLocal userService, NewsItemFacadeLocal newsItemFacade, SearchEngineLocal searchEngine) {
        this.cfgService = cfgService;
        this.newswireService = newswireService;
        this.notificationService = notificationService;
        this.userService = userService;
        this.newsItemFacade = newsItemFacade;
        this.searchEngine = searchEngine;
    }

    @Override
    public String getWorkingDirectory() {
        return cfgService.getString(ConfigurationKey.WORKING_DIRECTORY);
    }

    @Override
    public NewswireItem createNewswireItem(NewswireItem item) {
        return newswireService.create(item);
    }

    @Override
    public List<NewswireItem> findNewswireItemsByExternalId(String externalId) {
        return newswireService.findByExternalId(externalId);
    }

    @Override
    public void dispatchMail(String to, String from, String subject, String content) {
        notificationService.dispatchMail(to, from, subject, content);
    }

    @Override
    public Notification createNotification(Notification notifcation) {
        return notificationService.create(notifcation);
    }

    @Override
    public List<UserAccount> findUserAccountsByRole(String roleName) {
        return userService.findUserAccountsByUserRoleName(roleName);
    }

    @Override
    public List<NewsItem> findNewsItemsByStateAndOutlet(String stateName, Outlet outlet) {
        return newsItemFacade.findByStateAndOutlet(stateName, outlet);
    }

    @Override
    public void index(NewsItem item) throws SearchEngineIndexingException {
        try {
            searchEngine.index(item);
        } catch (SearchEngineIndexingException ex) {
            Logger.getLogger(PluginContextImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
